package data;

import java.util.ArrayList;
import java.util.List;

public class BasicLinked<E> implements Link<E>{
    private class Node<E> {
        private E e;
        private Node next;
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }

        public E getE() {
            return e;
        }

        public Node getNext() {
            return next;
        }
    }
    // 首节点
    private Node<E> head;
    // 链接节点个数
    private int size;

    public BasicLinked(){
        this.size = 0;
        this.head = null;
    }

    /**
     * 插入添加，找到位置，打断，引用更新上一节点的next 与新增节点的next
     * @param index
     * @param e
     */
    @Override
    public void add(int index, E e) {
        // 一开始是空的，第一次添加，则是添加首节点
        if(head == null){
            Node insertNode = new Node(e,null);
            this.head = insertNode;
            this.size++;
        } else if(head != null && head.next == null){
            // 首节点加数据
            Node previous = head;
            Node insertNode = new Node(e,previous);
            head = insertNode;
            this.size++;

        }else {
            // a->b->c->null
            int i = 0;
            Node current = head.next;
            while(current != null){
                Node previous = current;
                if(i == index){
                    Node next = previous.next;
                    Node insertNode = new Node(e,next);
                    previous.next = insertNode;
                }else{
                    current = current.next;
                }
                i++;
            }
            this.size++;
        }


    }

    @Override
    public void addFirst(E e) {
        this.add(0,e);
    }

    @Override
    public void addLast(E e) {

    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public E getFirst() {
        return this.head.getE();
    }

    @Override
    public E getLast() {
        // a->b->c->null
        if(head == null){
            return null;
        } else if(head != null && head.next == null){
            return head.getE();
        }else{
            Node<E> current = head.next;
            while(current != null){
                Node<E> previous = current;
                current = current.next;
                if(current == null){
                    return previous.getE();
                }
            }
        }
        return null;
    }

    public List<E> getAll() {
        List result = new ArrayList<>();
        if(head == null){
            return result;
        } else if(head != null){
            Node current = head;
            while (current != null){
                result.add(current.getE());
                current = current.next;
            }
        }
        return result;
    }

    @Override
    public void set(int index, E e) {

    }

    @Override
    public void remove(int index) {

    }

    @Override
    public void removeFisrt() {

    }

    @Override
    public void removeLast() {

    }





}
